<? 
	if(isset($_POST['btngo']))
	{
		require("required/dbconnect.php");
		$pageno=$_POST['pageno'];
		$main_cat=$_POST['main_cat'];
		$sub_cat=$_POST['sub_cat'];
		$keywordtype=$_POST['keyword_type'];
		$keyword=mysql_escape_string($_POST['keyword']);
		$price_from=$_POST['price_from'];
		$price_to=$_POST['price_to'];
		header("Location:os_search.php?page=$pageno&main_cat=$main_cat&sub_cat=$sub_cat&keyword=$keyword&keyword_type=$keywordtype&price_from=$price_from&price_to=$price_to");
	}
	require("required/dbconnect.php");
	require("required/share_function.php");
	$_SESSION['pid']=-1;
	require("required/header.php"); 
	
	if(empty($_GET['newsearch']))
	{
		if(empty($_GET['page']))
		{
			$page="1";
		}
		else
		{
			$page=$_GET['page'];
		}
	
		$totalperpage=15;

		$from=($page-1)*$totalperpage;	
		
		if(isset($_POST['keyword']))
		{
			$main_cat=$_POST['main_cat'];
			$main_cat_slt="";
			$sub_cat=$_POST['sub_cat'];
			$subcatslt="";
			$keywordtype=$_POST['keyword_type'];
			$keyword=mysql_escape_string($_POST['keyword']);
			
			
			if($main_cat=="")
			{
				$main_cat_q="";
			}
			else
			{
				$main_cat_q="and cat_id='$main_cat' ";
			}
			
			if($sub_cat=="" or $sub_cat=="-1")
			{
				$sub_cat_q="";
			}
			else
			{
				$sub_cat_q="and sub_cat_id='$sub_cat' ";
			}
			
			
						
			if($keyword!="")
			{
				if($keywordtype==1)
				{
					$keyword_q="and (product_name_eng like '%$keyword%' or product_name_eng like '%$keyword%')";
					$kt0="";
					$kt1="selected";
					$kt2="";
				}
				elseif($keywordtype==2)
				{
					$keyword_q="and (description_eng like '%$keyword%' or description_eng like '%$keyword%')";
					$kt0="";
					$kt1="";
					$kt2="selected";
				}
				else
				{
					$keyword_q="and (product_name_eng like '%$keyword%' or product_name_eng like '%$keyword%' or description_eng like '%$keyword%' or description_eng like '%$keyword%')";
					$kt0="selected";
					$kt1="";
					$kt2="";
				}
			}
			else
			{
				$keyword_q="";
				$kt0="";
				$kt1="";
				$kt2="";
			}
			
			
			$get_product=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q order by cat_id asc, sub_cat_id asc limit $from,$totalperpage")or die(mysql_error());
			$get_total=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q ")or die(mysql_error());
			
			//echo "select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q $price_q order by cat_id asc, sub_cat_id asc limit $from,$totalperpage";
		
		}
		else
		{
			if(empty($_GET['main_cat']))
			{
				$main_cat="";
				$main_cat_slt="selected";
				$main_cat_q="";
			}
			else
			{
				$main_cat=$_GET['main_cat'];
				$main_cat_slt="";
				$main_cat_q="and cat_id='$main_cat' ";
			}
			
			if(empty($_GET['sub_cat']))
			{
				$sub_cat="";
				$sub_cat_slt="selected";
				$sub_cat_q="";
			}
			else
			{
				$sub_cat=$_GET['sub_cat'];
				$sub_cat_slt="";
				if($sub_cat!='-1')
				{
					$sub_cat_q="and sub_cat_id='$sub_cat' ";
				}
				else
				{
					$sub_cat_q="";
				}
			}
			
				
					
			if(empty($_GET['keyword']))
			{
				$keyword="";
				$keyword_q="";
				$kt0="";
				$kt1="";
				$kt2="";
				$keywordtype="";
			}
			else
			{
				$keyword=$_GET['keyword'];
				$keywordtype=$_GET['keywordtype'];
				if($keywordtype==1)
				{
					$keyword_q="and (product_name_eng like '%$keyword%' or product_name_eng like '%keyword%')";
					$kt0="";
					$kt1="selected";
					$kt2="";
				}
				elseif($keywordtype==2)
				{
					$keyword_q="and (description_eng like '%$keyword%' or description_eng like '%keyword%')";
					$kt0="";
					$kt1="";
					$kt2="selected";
				}
				else
				{
					$keyword_q="and (product_name_eng like '%$keyword%' or product_name_eng like '%keyword%' or description_eng like '%$keyword%' or description_eng like '%keyword%')";
					$kt0="selected";
					$kt1="";
					$kt2="";
				}
				
			}
			
			$get_product=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q order by cat_id asc, sub_cat_id asc limit $from,$totalperpage")or die(mysql_error());
			$get_total=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q ")or die(mysql_error());
		}
		
		$count=mysql_num_rows($get_total);	
	
	
		$tr1="";
		$tr2="";
	
	?>
    
<link rel="stylesheet" href="required/coda-slider-2.0.css" type="text/css"/>
<script type="text/javascript" src="required/js/jquery-easing-1.3.pack.js"></script>
<script type="text/javascript" src="required/js/jquery.coda-slider-2.0.js"></script>
<link href="facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="facefiles/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox() 
    })
</script>
<script type="text/javascript">
  $().ready(function() {
	  <?
	  	for($i=0;$i<mysql_num_rows($get_product);$i++)
		{
	  ?>
	  $('#coda-slider-<?= $i ?>').codaSlider({dynamicArrows: false,autoSlide: true,autoSlideInterval: 3000,dynamicTabsAlign: 'left',dynamicTabsPosition : 'bottom'});
	  <? } ?>
  });
</script>
		
			<table width="100%" cellpadding="0" cellspacing="0">
						
				<td align="left" width="100%" valign="top">
                
				<table width="100%" cellpadding="5" cellspacing="0">
				<tr>
					<td class="page_title_bar" height="30" valign="middle">
						Search
					</td>
				</tr>		
				<tr>
                	
					<Td align="left" valign="top">
					<form action="os_search.php" method="post" name="frm">
                    <fieldset><legend>Advance Search</legend>
				<table width="531" cellpadding="3" cellspacing="0" >
				
				<script language="JavaScript" type="text/javascript" src="required/js/select_cat.js"></script>
				<?
					$get_cat=mysql_query("select * from os_category order by id asc");
				
				?>
				<tr>
					<Td width="20%">Category</td><td width="24%">
						<select name="main_cat" class="text" onChange="showSubCat(this.value)">
							<option value="" <?= $main_cat_slt ?>>All</option>
						<?
							while($c=mysql_fetch_array($get_cat,MYSQL_ASSOC))
							{
								if($c['id']==$main_cat)
								{
									$cslt="selected";
								}
								else
								{
									$cslt="";
								}
						?>
							<option value="<?= $c['id'] ?>" <?= $cslt ?>><?= $c['name_eng'] ?></option>
						<? } ?>
						</select>
						
					</td>
					<td width="24%">Sub category</td><td width="32%">
					
					<div id="divResult">
					<?
						if($main_cat!="")
						{
							$get_subcat=mysql_query("select * from os_sub_category where main_category=".$main_cat." order by id desc");
							if($sub_cat=='-1')
							{
								$soption="<option value='-1' selected>All</option>";
								$soption2="";
							}
							else
							{
								$soption="<option value=''>Please select</option>";
								$soption2="<option value='-1'>All</option>";
							}
					?>
							<select name="sub_cat" class="text">
							<?= $soption ?>
							<? 
								while($s=mysql_fetch_array($get_subcat,MYSQL_ASSOC))
								{
									if($s['id']==$sub_cat)
									{
										$sslt="selected";
									}
									else
									{
										$sslt="";
									}	
									?>
									<option <?= $sslt ?> value="<?= $s['id'] ?>"><?= $s['name_eng'] ?> </option>
									<? 
								
								}
							?>
							<?= $soption2 ?>
							</select>
					<?	
						}else{
					?>				
					
					<select name="sub_cat" class="text">
						<option value="">All</option>
					</select>		
					<? } ?>
				</div>
					</td>
				</tr>
				<tr>
					<td width="20%">Keyword</td><td><input type="text" name="keyword" size="20" maxlength="50" class="text" value="<?= $keyword ?>"></td>
					<td>Keyword type</td><td>
						<select name="keyword_type" class="text">
							<option value=""  <?= $kt0 ?>>All type</option>
							<option value="1" <?= $kt1 ?>>Product name</option>
							<option value="2" <?= $kt2 ?>>Product description</option>
						</select>
					</td>
				</tr>
				
				<tr>
					<td align="left"><input type="submit" value="Submit" class="button" name="btnsearch"></td>
				</tr>
			  </table>
              </fieldset>
			  </form>
              </Td>
				</tr>
				<tr>
				<Td  valign="top" width="965" >
					<br /><br />
                    <table align="center" width=100% cellpadding="0" cellspacing="0" >
					<?	
						if($count==0)
						{
					?>
						<tr>
							<td align="center" height="300" valign="middle"> :: NO RECORD :: </td>
						</tr>
					<?	
						}
						else
						{
					?>
					<tr>
						<td  colspan="6" align="center" valign="top">
							<table width="100%" cellpadding="0" cellspacing="0" >
								<td width="69%" height="45" align="left">
								<?= getPaging($totalperpage,$count,$page,"os_search.php?param=ntg") ?>
								
								</td>
								
							</table>
						</td>
					</tr>
                    <Tr>
                    	<td colspan="6">
                        	<table>
							<?	productAvatar($get_product); ?>
                    		</table>
                         </td>
                    </Tr>
                    <tr>
						<td  colspan="6" align="left" valign="top">
							<table width="100%" cellpadding="0" cellspacing="0" >
								<td width="69%" height="45" align="left">
								<?= getPaging($totalperpage,$count,$page,"os_search.php?param=ntg") ?>
								
								</td>
								
							</table>
						</td>
					</tr>
					<?
						}
					?>
					</table>
				</Td>
			</tr>		  
			</table>
			</td>
		</table>			
		
<?
	}
	else
	{
?>

			<table width="100%">
			<td rowspan="2" valign="top">
						<? require("required/product_category.php"); ?>
						</td>
				<td align="left" width="100%" valign="top">
					<table width="100%">
						
						<td height="73" align="right">
							<table id="Table_01" height="45" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="13" rowspan="3" align="right">
										<img src="images/product_title_01.jpg" width="13" height="45" alt=""></td>
									<td width="263" height="10" style="background:url('images/product_title_02.jpg');background-repeat:repeat-x"></td>
									<td width="28" rowspan="3" align="left">
										<img src="images/product_title_03.jpg" width="11" height="45" alt=""></td>
								</tr>
								<tr>
									<td background="images/product_title_04.jpg" height="26">
										Current position <img src="images/arrow.gif"> <font style="font-size:12px;font-weight:bolder"><?= $mainpage ?></font> <img src="images/arrow.gif"> <font style="font-size:12px;font-weight:bolder">Product search</font> 
									</td>
								</tr>
								<tr>
									<td style="background:url('images/product_title_05.jpg');background-repeat:repeat-x" height="9"></td>
								</tr>
							</table>
						<br>
						</td>						
					</table>
					<form action="os_search.php" method="post" name="frm">
				<table width="100%" cellpadding="3" cellspacing="0" class="solidborder_black" align="center">
				<tr  height="30">
				<td colspan="5">Product search</td>
				</tr>
				<script language="JavaScript" type="text/javascript" src="required/js/select_cat.js"></script>
				<?
					$get_cat=mysql_query("select * from os_category order by id asc");
				
				?>
				<tr>
					<Td width="20%">Category</td><td width="24%">
						<select name="main_cat" class="text" onChange="showSubCat(this.value)">
							<option value="" >All</option>
						<?
							while($c=mysql_fetch_array($get_cat,MYSQL_ASSOC))
							{
								
						?>
							<option value="<?= $c['id'] ?>"><?= $c['name_eng'] ?></option>
						<? } ?>
						</select>
						
					</td>
					<td width="24%">Sub Category</td><td width="32%">
					
					<div id="divResult">
					
					
					<select name="sub_cat" class="text">
						<option value="">All</option>
					</select>		
					
				</div>
					</td>
				</tr>
				<tr>
					<td width="20%">Keyword</td><td><input type="text" name="keyword" size="20" maxlength="50" class="text" value=""></td>
					<td>Keyword type</td><td>
						<select name="keyword_type" class="text">
							<option value=""  >All type</option>
							<option value="1" >Product name</option>
							<option value="2" >Product description</option>
						</select>
					</td>
				</tr>
				<tr>
					<td>Price range</td><td><input type="text" class="text" size="2" name="price_from" value=""> - <input type="text" class="text" size="2" name="price_to" value=""></td>
				</tr>
				<tr>
					<td align="center"><input type="submit" value="Submit" class="button" name="btnsearch"></td>
				</tr>
			  </table>
			  </form>
				</td>
				
			</table>			
		
	
<? }require("required/footer.php") ?>		